package com.gedcorp.gedmanager;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.gedmanager.db.Cost;
import com.gedmanager.db.CostsDataSource;

/**
 * A fragment that launches other parts of the demo application.
 */
public class CostHistorySectionFragment extends Fragment {

	private CostsDataSource datasource;
	static ArrayList<ListviewCostItem> listCost;
	static ListView lv;
	static ListviewCostAdapter lvAdapter;
	static View rootView;
	
	
	public void refreshes(ArrayList<ListviewCostItem> list, Context context) {
		listCost = list;
		lv = (ListView) rootView.findViewById(R.id.listviewcost);
		lvAdapter = new ListviewCostAdapter(context, listCost);
		lv.setAdapter(lvAdapter);
		lvAdapter.notifyDataSetChanged();

		lv.refreshDrawableState();
		lv.invalidate();

		Log.d("DDD", "----------");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (!MainActivity.hasValidLicenseValue()) {
			rootView = inflater.inflate(R.layout.fragment_section_empty, container, false);
			return rootView;
		}
		rootView = inflater.inflate(R.layout.fragment_section_cost_history, container, false);

		listCost = GetlistCost();
		lv = (ListView) rootView.findViewById(R.id.listviewcost);
		lv.setAdapter(new ListviewCostAdapter(getActivity(), listCost));

		Button btn = (Button) rootView.findViewById(R.id.new_cost_button);

		// imv = (ImageView)findViewById(R.id.imv);

		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				FragmentManager fm = getActivity().getSupportFragmentManager();
				NewCostDialog newCostDialog = new NewCostDialog();
				newCostDialog.show(fm, "custom_dialog_new_cost");

			}
		});
		
		Button btnFind = (Button) rootView.findViewById(R.id.find_cost_button);

		// imv = (ImageView)findViewById(R.id.imv);

		btnFind.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				FragmentManager fm = getActivity().getSupportFragmentManager();
				FindCostDialog findCostDialog = new FindCostDialog();
				findCostDialog.show(fm, "custom_dialog_find_cost");

			}
		});
		return rootView;
	}

	private ArrayList<ListviewCostItem> GetlistCost() {
		ArrayList<ListviewCostItem> contactlist = new ArrayList<ListviewCostItem>();

		datasource = new CostsDataSource(getActivity());
		datasource.open();

		List<Cost> values = datasource.getAllCosts();

		for (Cost temp : values) {
			ListviewCostItem contact = new ListviewCostItem();
			contact.SetId(temp.getId());
			contact.SetName(temp.getName());
			contact.SetDate(temp.getDate());
			contact.SetValue(temp.getValue());
			contact.SetComment(temp.getComment());
			contactlist.add(contact);
		}
		datasource.close();



		return contactlist;
	}

}